﻿/*
The MIT License (MIT)
Copyright (c) 2013 kaveh.shahbazian@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.
*/
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Buddy
{
    public class Ini
    {
        string _path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public Ini() : this("conf") { }
        public Ini(string iniFileName) : this(iniFileName, false) { }
        public Ini(string iniFilePath, bool isFullPath)
        {
            if (isFullPath) _path = iniFilePath;
            else _path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, (string.IsNullOrWhiteSpace(iniFilePath) ? "conf" : iniFilePath) + ".ini");
        }

        /// <summary>
        /// uses the default section named 'conf'
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string this[string key]
        {
            get { return this["conf", key]; }
            set { this["conf", key] = value; }
        }

        public string this[string section, string key]
        {
            get { return Get(section, key); }
            set { Set(section, key, value); }
        }

        public void DeleteSection(string section)
        {
            WritePrivateProfileString(section, null, null, this._path);
        }

        void Set(string section, string key, string @value)
        {
            WritePrivateProfileString(section, key ?? string.Empty, @value ?? string.Empty, this._path);
        }

        string Get(string section, string key)
        {
            var bufferLength = 4096;

            StringBuilder temp = new StringBuilder(bufferLength);
            int i = GetPrivateProfileString(section, key, string.Empty, temp, bufferLength, this._path);
            return temp.ToString();
        }
    }
}
